<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class AccountIntegralModel extends AbstractModel
{
    protected $tableName = 't_account_integral';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getAccount($where)
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addAccount($where)
    {
        try {
            $model = new UserAccountModel();
            $account = $model->getUser(['uid' => $where['uid']]);
            if (!$account) {
                return false;
            }
            // 开启事务
            \EasySwoole\ORM\DbManager::getInstance()->startTransaction();

            $this->data($where, false);
            $res = $this->save();
            if ($where['bill_type'] == 1){
                $amount = $account->integral + $where['amount'];
            }else{
                $amount = $account->integral - $where['amount'];
            }
            if ($amount < 0){
                return false;
            }
            $model->update(['integral' => $amount], ['uid' => $where['uid']]);

            // 提交事务
            \EasySwoole\ORM\DbManager::getInstance()->commit();

            return $res;
        } catch (Throwable $e) {
            // 回滚事务
            \EasySwoole\ORM\DbManager::getInstance()->rollback();
            return false;
        }
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('用户积分账户表');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colInt('uid')->setDefaultValue(0)->setIsNotNull()->setColumnComment('用户id');
        $table->colInt('bill_type')->setDefaultValue(0)->setColumnComment("账单类型：0.出账，1.入账");
        $table->colInt('mold')->setDefaultValue(0)->setColumnComment("0:消费，1：充值，2：兑换");
        $table->colVarChar('amount', 25)->setDefaultValue('')->setColumnComment('金额');
        $table->colVarChar('static', 25)->setDefaultValue('')->setColumnComment('易统计数据');
        $table->colVarChar('memo', 25)->setDefaultValue('')->setColumnComment('备注');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('uid_index', 'uid');
        $table->indexNormal('bill_type_index', 'bill_type');
        $table->indexNormal('amount_index', 'amount');
        $table->indexNormal('static_index', 'static');

        return $table;
    }
}